using JGSY.CMS.LowCode.Platform.Application.DTOs.Monitoring;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces.Monitoring;

/// <summary>
/// 监控服务接口
/// 提供性能指标记录、错误日志记录、用户交互追踪和监控数据分析功能
/// </summary>
public interface IMonitoringService
{
    /// <summary>
    /// 批量记录性能指标
    /// </summary>
    /// <param name="metrics">性能指标列表</param>
    /// <returns>记录结果</returns>
    Task<bool> RecordPerformanceMetricsAsync(List<PerformanceMetricDto> metrics);

    /// <summary>
    /// 记录错误日志
    /// </summary>
    /// <param name="error">错误信息</param>
    /// <returns>记录结果</returns>
    Task<bool> RecordErrorAsync(ErrorLogDto error);

    /// <summary>
    /// 记录用户交互
    /// </summary>
    /// <param name="interaction">交互信息</param>
    /// <returns>记录结果</returns>
    Task<bool> RecordUserInteractionAsync(UserInteractionDto interaction);

    /// <summary>
    /// 获取监控仪表板数据
    /// </summary>
    /// <param name="startDate">开始日期</param>
    /// <param name="endDate">结束日期</param>
    /// <returns>仪表板数据</returns>
    Task<MonitoringDashboardDto> GetDashboardDataAsync(DateTime? startDate = null, DateTime? endDate = null);

    /// <summary>
    /// 获取错误汇总信息
    /// </summary>
    /// <param name="limit">限制数量</param>
    /// <returns>错误汇总列表</returns>
    Task<List<RecentErrorDto>> GetErrorSummaryAsync(int limit = 50);

    /// <summary>
    /// 获取会话详情
    /// </summary>
    /// <param name="sessionId">会话ID</param>
    /// <returns>会话详情</returns>
    Task<SessionDetailDto> GetSessionDetailAsync(string sessionId);

    /// <summary>
    /// 清理过期数据
    /// </summary>
    /// <param name="retentionDays">数据保留天数</param>
    /// <returns>清理数量</returns>
    Task<int> CleanupExpiredDataAsync(int retentionDays = 90);

    /// <summary>
    /// 获取性能趋势数据
    /// </summary>
    /// <param name="metricName">指标名称</param>
    /// <param name="hours">小时数</param>
    /// <returns>趋势数据</returns>
    Task<List<PerformanceTrendDto>> GetPerformanceTrendsAsync(string metricName, int hours = 24);

    /// <summary>
    /// 大批量数据处理
    /// </summary>
    /// <param name="bulkData">大批量数据</param>
    /// <returns>处理结果</returns>
    Task<bool> ProcessBulkDataAsync(BulkDataDto bulkData);

    /// <summary>
    /// 获取页面分析数据
    /// </summary>
    /// <param name="timeRange">时间范围</param>
    /// <param name="page">指定页面</param>
    /// <returns>页面分析数据</returns>
    Task<List<PageAnalysisDto>> GetPageAnalysisAsync(string timeRange = "24h", string? page = null);

    /// <summary>
    /// 获取用户行为分析数据
    /// </summary>
    /// <param name="userId">用户ID</param>
    /// <param name="timeRange">时间范围</param>
    /// <returns>用户分析数据</returns>
    Task<List<UserAnalysisDto>> GetUserAnalysisAsync(string? userId = null, string timeRange = "24h");

    /// <summary>
    /// 获取实时监控数据
    /// </summary>
    /// <returns>实时数据</returns>
    Task<RealtimeDataDto> GetRealtimeDataAsync();

    /// <summary>
    /// 获取监控配置
    /// </summary>
    /// <returns>监控配置</returns>
    Task<MonitoringConfigDto> GetMonitoringConfigAsync();

    /// <summary>
    /// 更新监控配置
    /// </summary>
    /// <param name="config">监控配置</param>
    /// <returns>更新结果</returns>
    Task<bool> UpdateMonitoringConfigAsync(MonitoringConfigDto config);

    /// <summary>
    /// 获取系统状态
    /// </summary>
    /// <returns>系统状态</returns>
    Task<SystemStatus> GetSystemStatusAsync();
}

/// <summary>
/// 会话详情数据传输对象
/// </summary>
public class SessionDetailDto
{
    /// <summary>
    /// 会话ID
    /// </summary>
    public string SessionId { get; set; } = string.Empty;

    /// <summary>
    /// 用户ID
    /// </summary>
    public string? UserId { get; set; }

    /// <summary>
    /// 会话开始时间
    /// </summary>
    public DateTime SessionStart { get; set; }

    /// <summary>
    /// 会话结束时间
    /// </summary>
    public DateTime? SessionEnd { get; set; }

    /// <summary>
    /// 会话时长（分钟）
    /// </summary>
    public int? SessionDuration { get; set; }

    /// <summary>
    /// 页面浏览数
    /// </summary>
    public int PageViews { get; set; }

    /// <summary>
    /// 错误数量
    /// </summary>
    public int ErrorCount { get; set; }

    /// <summary>
    /// 性能指标
    /// </summary>
    public Dictionary<string, decimal> PerformanceMetrics { get; set; } = new();

    /// <summary>
    /// 浏览器信息
    /// </summary>
    public string? BrowserName { get; set; }

    /// <summary>
    /// 设备类型
    /// </summary>
    public string? DeviceType { get; set; }

    /// <summary>
    /// 访问页面列表
    /// </summary>
    public List<string> VisitedPages { get; set; } = new();

    /// <summary>
    /// 错误列表
    /// </summary>
    public List<SessionErrorDto> Errors { get; set; } = new();
}

/// <summary>
/// 会话错误信息
/// </summary>
public class SessionErrorDto
{
    /// <summary>
    /// 错误ID
    /// </summary>
    public long Id { get; set; }

    /// <summary>
    /// 错误类型
    /// </summary>
    public string ErrorType { get; set; } = string.Empty;

    /// <summary>
    /// 错误消息
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 页面URL
    /// </summary>
    public string PageUrl { get; set; } = string.Empty;

    /// <summary>
    /// 严重程度
    /// </summary>
    public string Severity { get; set; } = string.Empty;

    /// <summary>
    /// 发生时间
    /// </summary>
    public DateTime OccurredAt { get; set; }
}
